package On_class.L0331.book;


import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class BooksDaoHashMap implements BooksDao {
    HashMap<Integer,Books> map=new HashMap<Integer, Books>();
    @Override
    public void insert(Books books) {
        Books b=select(books.getId());
        if(b==null){
            map.put(books.getId(),books);

        }else{
            System.out.println("保存失败，id重复");
        }

    }

    @Override
    public void selectAll() {
        System.out.println("所有图书信息如下：");
        Iterator<Map.Entry<Integer,Books>> iterator=map.entrySet().iterator();

        Map.Entry<Integer,Books>entry=iterator.next();

        System.out.println(entry.getValue());

    }

    @Override
    public Books select(int id) {
        Iterator<Integer> iterator=map.keySet().iterator();
        while (iterator.hasNext()){
            Integer key=iterator.next();
            if (key==id){
                return map.get(key);
            }
        }
        return null;
    }

    @Override
    public void update(int id, Books b) {

    }

    @Override
    public void delete(int id) {

    }
}
